package Commer;
import number.Input;

import javax.swing.*;
import java.awt.*;
import java.awt.event.AWTEventListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class GlobalMouseClickPosition {

    private JFrame frame;
    private JTextArea positionTextArea;

    private static JFrame frameZhi;
    private static JPanel panelZhi;

    public GlobalMouseClickPosition() {

        // 初始化窗口
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setLayout(new BorderLayout());
        frame.setAlwaysOnTop(true);

        // 创建一个JPanel，用于放置点击面板
        zhi();

        // 创建提示标签
        JLabel promptLabel = new JLabel("点击桌面，位置将显示在这里");
        promptLabel.setFont(new java.awt.Font("微软雅黑", 0, 24));
        promptLabel.setHorizontalAlignment(SwingConstants.CENTER);
        frame.add(promptLabel, BorderLayout.NORTH);

        //设置JLabel背景色透明
        promptLabel.setOpaque(false);

        // 创建文本域
        positionTextArea = new JTextArea();
        positionTextArea.setEditable(false);
        positionTextArea.setFont(new java.awt.Font("微软雅黑", 0, 24));
        frame.add(new JScrollPane(positionTextArea), BorderLayout.CENTER);


        // 添加全局鼠标事件监听器
        Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
            @Override
            public void eventDispatched(AWTEvent event) {
                if (event instanceof MouseEvent) {
                    MouseEvent mouseEvent = (MouseEvent) event;
                    if (mouseEvent.getID() == MouseEvent.MOUSE_CLICKED) {
                        // 获取鼠标点击的位置
                        int x = mouseEvent.getXOnScreen();
                        int y = mouseEvent.getYOnScreen();
                        // 更新文本域
                        positionTextArea.append("点击位置: (" + x + ", " + y + ")\n");
                    }
                }
            }
        }, AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK);

        // 显示窗口
        frame.setVisible(true);
    }

    public static void zhi(){
        // 创建 JFrame 并设置属性
        frameZhi = new JFrame();
        frameZhi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frameZhi.setSize(100, 110);
        frameZhi.setLocation(500, 500);

        // 设置无边框
        frameZhi.setUndecorated(true);

        // 设置始终在最上方
        frameZhi.setAlwaysOnTop(true);

        // 设置透明背景
        frameZhi.setBackground(new Color(0, 0, 0, 0));
        frameZhi.setOpacity(0.9f); // 设置透明度

        // 创建 JPanel 并设置布局管理器
        panelZhi = new JPanel();
        panelZhi.setLayout(new FlowLayout());
        panelZhi.setOpaque(false); // 设置 JPanel 为透明

        // 创建图片并将其放入 JLabel 中
        ImageIcon icon = new ImageIcon("zhi.png");
        JLabel label = new JLabel(icon);
        panelZhi.add(label);

        // 将 JPanel 添加到 JFrame 中
        frameZhi.add(panelZhi);
        //设置窗口可移动
        frameZhi.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                int x = e.getXOnScreen() - frameZhi.getWidth() / 2;
                int y = e.getYOnScreen() - frameZhi.getHeight() / 2;
                frameZhi.setLocation(x, y);
            }
        });
        frameZhi.setVisible(true);

    }

}